<?php

class verifyCodeClaimModule extends Module {
	/*
	 * 在构造函数中装载所依赖的类或其他文件
	*/
	function __construct() {
		$this->includePhp('verifyCodeClaim', 'verifyCodeClaim');
	}
	/*
	 * 激活创建数据库
	*/
	public function activate() {
		Module::importSQL(module_sql_path('verifyCodeClaim'));
	}
	
	public function run($message, $material, $userStatus) {	
		//$VerifyCode = M("module_verify_code");
// 		$CustomerUser = new Model();
		
		
		$cid = 	Session::$user['customerId'];
		//$mid = $material->id;
		
		$materialObj = simplexml_load_string($material->xml,
				'SimpleXMLElement', LIBXML_NOCDATA);
		// 获取素材欢迎语中的欢迎语和感谢语
		$materialExit = $materialObj->exit; 
		$materialInputUserName = $materialObj->inputusername; 
		$materialErrorInputUserName = $materialObj->errorinputusername; 
		$materialInputPassword = $materialObj->inputpassword; 
		$materialErrorInputPassword = $materialObj->errorinputpassword; 
		$materialErrorUserNotFound = $materialObj->errorusernotfound; 
		$materialInputVerifyCode = $materialObj->inputverifycode; 
		$materialErrorInputVerifyCode = $materialObj->errorinputverifycode; 
		$materialErrorVerifyCodeNotFound = $materialObj->errorverifycodenotfound; 
		$materialErrorVerifyCodeIsClaim = $materialObj->errorverifycodeisclaim; 
		$materialVerifyCodeIsClaim = $materialObj->verifycodeisclaim; 
		
		// 所有回复均为文本
		$rm = $message->reply(message_type_text);
		
		// 任何时候只要输入q，就退出结束流程
		if (strcasecmp( trim($message->content), "q") == 0 ) {
			$userStatus->reserve1 = '';
			$userStatus->status = '0';
			UserStatus::end($userStatus);
			$rm->content = $materialExit;
			return $rm;
		}
		
				
		// 用户刚进入的时候状态为空字符串，发送欢迎语，并更改状态为1
		if (!isset($userStatus)) {
			$us = UserStatus::create($message->fromUserName, $material->id);
			$us->status = '1';
			UserStatus::begin($us);
			$rm->content = $materialInputUserName;
			//$rm->funcFlag = 1;//加星标
			return $rm;
		} 
		
		
		
		switch ($userStatus->status) {
			case '1':
				//判断是否是text，如果不是提示错误，是记录用户姓名，提示输入手机号
				if($message->msgType!=message_type_text) {
					$rm->content = $materialErrorInputUserName;
					break;
				}
				// 切换客户
				Session::switchCustomer(0);
				//Session::switchCustomer($cid);
				$CustomerUser = M("customer_user");
				$conditionName['email'] = array('eq',trim($message->content));
				$conditionName['customerId'] = array('eq',$cid);
				$conditionName['agent'] = array('eq','service');
				$resultUser = $CustomerUser->where($conditionName)->select();
				Session::switchCustomer($cid);
				
// 				$sql = 'select * from wx_customer_user where customerId = '.$cid
// 						.' and  email = "'.trim($message->content).'"';
				
// 				$resultUser = $CustomerUser->query($sql);
				
				Log::write('verify code claim query userName sql is:'.$CustomerUser->getLastSql());
				
				if(!$resultUser){
					$rm->content = $materialErrorInputUserName;
					break;
				}
				
// 				// 判断是否是wx1-wx25,不是则返回错误
// 				$arr = explode("wx",$message->content);
// 				if (!((count($arr)==2)&&((int)$arr[1]>0)&&((int)$arr[1]<26)))
// 				{
// 					$rm->content = $materialErrorInputUserName;
// 					break;
// 				}
				// 存入登录名,将登录名存入预留字段中
				$userStatus->reserve1 = $message->content;
				// 存入状态
				$userStatus->status = '2';
				UserStatus::update($userStatus);
				$rm->content = $materialInputPassword;
				break;
			case '2':
				//判断是否是text，如果不是提示错误
				if($message->msgType!=message_type_text) {
					$rm->content = $materialErrorInputPassword;
					break;
				}
				
				// 切换客户
				Session::switchCustomer(0);
				//Session::switchCustomer($cid);
				$CustomerUser = M("customer_user");
				$conditionPsw['email'] = array('eq', trim($userStatus->reserve1));
				$conditionPsw['password'] = array('eq', Encryption::password(trim($message->content)));
				$conditionPsw['customerId'] = array('eq',$cid);
				$conditionName['agent'] = array('eq','service');
				$resultUser = $CustomerUser->where($conditionPsw)->select();
				Session::switchCustomer($cid);
				if(!$resultUser){
					$rm->content = $materialErrorInputPassword;
					break;
				}
				Log::write('verify code claim query psw sql is:'.$CustomerUser->getLastSql());
				
// 				// 判断密码是否wx321,不是返回错误
// 				if($message->content!="wx321"){
// 					$rm->content = $materialErrorInputPassword;
// 					break;
// 				}
				
				
				// 存入状态,提示输入验证码
				$userStatus->status = '3';
				UserStatus::update($userStatus);
				$rm->content = $materialInputVerifyCode;			
				break;
			case '3':
				//判断是否是text，如果不是提示错误
				if($message->msgType!=message_type_text) {
					$rm->content = $materialErrorInputVerifyCode;
					break;
				}
				// 调用action，先检查该验证码是否存在
				$resultCode = verifyCodeClaim::verify($message->content);
// 				$result = R('VerifyCode/verify',array($message->content));
				// 不存在该验证码，提示该验证码不存在
				if($resultCode == code_verifycode_notexist) {
					$rm->content = $materialErrorVerifyCodeNotFound;
					break;
				} else if($resultCode == code_verifycode_claimed) {
					$rm->content = $materialErrorVerifyCodeIsClaim;
					break;
				}
				// 取出当前用户刚才登录的账号wx1-wx25
				$loginName = $userStatus->reserve1; // 需要在数据库和框架中增加的字段
				// 领用
				$resultCode = verifyCodeClaim::claim($message->content, $loginName);
// 				$result = R('VerifyCode/claim',array($message->content,$loginName));
				// 存在该验证码，但是已经被领用，提示已经被领用
				if($resultCode == code_verifycode_notexist) {
					$rm->content = $materialErrorVerifyCodeNotFound;
					break;
				} else if($resultCode == code_verifycode_claimed) {
					$rm->content = $materialErrorVerifyCodeIsClaim;
					break;
				}
				
				$VerifyCode = M("module_verify_code");
				$event = $VerifyCode->where("code='$message->content'")->getField('reward');
				$xml = str_replace('{event}', $event, $materialVerifyCodeIsClaim);
				// 存在该验证码，领用，提示成功
				$rm->content = $xml;
				
				break;
		}
		
		return $rm;
		
		
	}
	
}

$module = new verifyCodeClaimModule();
Module::addModule($module);
?>